#include<iostream.h>

#include<TROOT.h>
#include<TFile.h>
#include<TTree.h>
#include<TMath.h>
#include<TCanvas.h>
#include<TH1F.h>
#include<TH2F.h>
#include<TF1.h>
#include<TStyle.h>
#include<TApplication.h>

#include<Global.h>

int main(int argc, char* argv[]){
  
  gStyle->SetOptFit(1);
  gStyle->SetPalette(1);

  Int_t RunNumber = atoi(argv[1]);

  TApplication app("myapp", &argc, argv);

  Global* glob;
  glob = new Global(RunNumber);

  Float_t DigX[glob->TELNumberOfPlanes][glob->TELMaxHit];
  Float_t DigY[glob->TELNumberOfPlanes][glob->TELMaxHit];
  Float_t CogX[glob->TELNumberOfPlanes][glob->TELMaxHit];
  Float_t CogY[glob->TELNumberOfPlanes][glob->TELMaxHit];

  cerr << " O: Number of planes = " << glob->TELNumberOfPlanes << " : Number of Hits = " << glob->TELMaxHit << endl ;
  
  Char_t Path[200];
  sprintf(Path,"../../ntuple/tele/TELHits%u.root",RunNumber);

  TFile *trackfile;
  trackfile = new TFile(Path);
  trackfile->cd();

  TTree *Tracks = (TTree*)trackfile->Get("TELHits");

  Tracks->SetBranchAddress("DigX",&DigX[0][0]);
  Tracks->SetBranchAddress("DigY",&DigY[0][0]);
  Tracks->SetBranchAddress("CogX",&CogX[0][0]);
  Tracks->SetBranchAddress("CogY",&CogY[0][0]);

  //Residuals Dig
  TH1F* h11[2][glob->TELNumberOfPlanes];
  //o//TCanvas* canv11 = new TCanvas("c1","",1200,800);
  //o//canv11->SetFillColor(0);
  //o//canv11->Divide(glob->TELNumberOfPlanes,2);

  //Residuals Cog
  TH1F* h12[2][glob->TELNumberOfPlanes];
  //o//TCanvas* canv12 = new TCanvas("c2","",1200,800);
  //o//canv12->SetFillColor(0);
  //o//canv12->Divide(glob->TELNumberOfPlanes,2);

  //Residuals Cog
  TH2F* h21[2][glob->TELNumberOfPlanes];
  //o//TCanvas* canv21 = new TCanvas("c3","",1200,800);
  //o//canv21->SetFillColor(0);
  //o//canv21->Divide(glob->TELNumberOfPlanes,2);
  
  Char_t TELRoot[100];

  //o// Booking Histogramms
  for(Int_t n = 0; n < glob->TELNumberOfPlanes; n++){

    sprintf(TELRoot,"ResDig_X_%u",n+1);
    h11[0][n] = new TH1F(TELRoot,"",100,-glob->TELCorrIntervalX[0],glob->TELCorrIntervalX[0]); 
    h11[0][n]->GetYaxis()->SetTitle("Number of events");
    h11[0][n]->GetXaxis()->SetTitle("Res_{X} [#mum]");

    sprintf(TELRoot,"ResDig_Y_%u",n+1);
    h11[1][n] = new TH1F(TELRoot,"",100,-glob->TELCorrIntervalY[0],glob->TELCorrIntervalY[0]);
    h11[1][n]->GetYaxis()->SetTitle("Number of events");
    h11[1][n]->GetXaxis()->SetTitle("Res_{Y} [#mum]");

    sprintf(TELRoot,"ResCog_X_%u",n+1);
    h12[0][n] = new TH1F(TELRoot,"",100,-glob->TELCorrIntervalX[0],glob->TELCorrIntervalX[0]); 
    h12[0][n]->GetYaxis()->SetTitle("Number of events");
    h12[0][n]->GetXaxis()->SetTitle("Res_{X} [#mum]");

    sprintf(TELRoot,"ResCog_Y_%u",n+1);
    h12[1][n] = new TH1F(TELRoot,"",100,-glob->TELCorrIntervalY[0],glob->TELCorrIntervalY[0]);
    h12[1][n]->GetYaxis()->SetTitle("Number of events");
    h12[1][n]->GetXaxis()->SetTitle("Res_{Y} [#mum]");

    sprintf(TELRoot,"Res_X_ver_Y_%u",n+1);
    h21[0][n] = new TH2F(TELRoot,"",glob->TELNumberOfStripsY[n],0,glob->TELNumberOfStripsY[n] * glob->TELStripSizeY[n],100,-glob->TELCorrIntervalX[0],glob->TELCorrIntervalX[0]); 
    h21[0][n]->GetYaxis()->SetTitle("Res_{X} [#mum]");
    h21[0][n]->GetXaxis()->SetTitle("Y [#mum]");

    sprintf(TELRoot,"Res_Y_ver_X_%u",n+1);
    h21[1][n] = new TH2F(TELRoot,"",glob->TELNumberOfStripsX[n],0,glob->TELNumberOfStripsX[n] * glob->TELStripSizeX[n],100,-glob->TELCorrIntervalY[0],glob->TELCorrIntervalY[0]);
    h21[1][n]->GetYaxis()->SetTitle("Res_{Y} [#mum]");
    h21[1][n]->GetXaxis()->SetTitle("X [#mum]");

  }

  Int_t while1;
  Int_t while2;


  //o// read events
  //for(Int_t i = 0; i < Tracks->GetEntries(); i++){
  for(Int_t i = 0; i < 10; i++){
    // loop over entries
    Tracks->GetEntry(i);
    cerr << " Event = " << i << endl;
    while2 = 0;

    //for(Int_t n = 0; n < glob->TELNumberOfPlanes - 1; n++){ //loop over hits in 1 and second plane
    for(Int_t n = 0; n < glob->TELNumberOfPlanes ; n++){ //loop over hits in 1 and second plane
      //o//for(Int_t m = n+1; m < glob->TELNumberOfPlanes; m++){ //loop over hits in 2 and 3 plane
	
	// read hits in all planes 
	while1 = 0;
	while(DigX[n][while1] > 0 && while1 < glob->TELMaxHit){
	  cerr << "Dig[" << n << "][" << while1 << "] = " << DigX[n][while1] << endl;

	  while1++;
	}
	
	/* //o//
	while1 = 0;
	while(DigX[n][while1] > 0 && DigX[m][while1] > 0 && while1 < glob->TELMaxHit){
	  cerr << "Dig[" << n << "][" << while1 << "] = " << DigX[n][while1] << 
	  //o//h11[0][while2]->Fill(DigX[n][while1] - DigX[m][while1]);
	  //o//sprintf(TELRoot,"Dig_X_%u_%u",n+1,m+1);
	  //o//h11[0][while2]->SetTitle(TELRoot);
	  //h21[0][while2]->Fill(DigY[n][while1],DigX[n][while1] - DigX[m][while1]);
	  //sprintf(TELRoot,"TEL_%u_%u",n+1,m+1);
	  //h21[0][while2]->SetTitle(TELRoot);
	  while1++;
	}

	while1 = 0;
	while(DigY[n][while1] > 0 && DigY[m][while1] > 0 && while1 < glob->TELMaxHit){
	  //o//h11[1][while2]->Fill(DigY[n][while1] - DigY[m][while1]);
	  //o//sprintf(TELRoot,"Dig_Y_%u_%u",n+1,m+1);
	  //o//h11[1][while2]->SetTitle(TELRoot);
	  //h21[1][while2]->Fill(DigX[n][while1],DigY[n][while1] - DigY[m][while1]);
	  //sprintf(TELRoot,"TEL_%u_%u",n+1,m+1);
	  //h21[1][while2]->SetTitle(TELRoot);
	  while1++;
	}

	while1 = 0;
	while(CogX[n][while1] > 0 && CogX[m][while1] > 0 && CogY[n][while1] > 0 && while1 < glob->TELMaxHit){
	  //o//h12[0][while2]->Fill(CogX[n][while1] - CogX[m][while1]);
	  //o//sprintf(TELRoot,"Cog_X_%u_%u",n+1,m+1);
	  //o//h12[0][while2]->SetTitle(TELRoot);
	  //o//h21[0][while2]->Fill(CogY[n][while1],CogX[n][while1] - CogX[m][while1]);
	  //o//sprintf(TELRoot,"TEL_%u_%u",n+1,m+1);
	  //o//h21[0][while2]->SetTitle(TELRoot);
	  while1++;
	}

	while1 = 0;
	while(CogY[n][while1] > 0 && CogY[m][while1] > 0 && CogX[n][while1] > 0 && while1 < glob->TELMaxHit){
	  //o//h12[1][while2]->Fill(CogY[n][while1] - CogY[m][while1]);
	  //o//sprintf(TELRoot,"Cog_Y_%u_%u",n+1,m+1);
	  //o//h12[1][while2]->SetTitle(TELRoot);
	  //o//h21[1][while2]->Fill(CogX[n][while1],CogY[n][while1] - CogY[m][while1]);
	  //o//sprintf(TELRoot,"TEL_%u_%u",n+1,m+1);
	  //o//h21[1][while2]->SetTitle(TELRoot);
	  while1++;
	}
	*/ //0//

	while2++;

	//o// }//loop over m
    }// loop over n

  }//i


  //Draw all histogramms
  /* for(Int_t n = 0; n < glob->TELNumberOfPlanes; n++){

    canv11->cd(n+1);
    h11[0][n]->Draw();
    h11[0][n]->Fit("gaus");
    canv11->cd(n + 1  + glob->TELNumberOfPlanes);
    h11[1][n]->Draw();
    h11[1][n]->Fit("gaus");

    canv12->cd(n+1);
    h12[0][n]->Draw();
    h12[0][n]->Fit("gaus");
    canv12->cd(n + 1 + glob->TELNumberOfPlanes);
    h12[1][n]->Draw();
    h12[1][n]->Fit("gaus");

    canv21->cd(n+1);
    canv21->GetPad(n+1)->SetGridx();
    canv21->GetPad(n+1)->SetGridy();
    h21[0][n]->Draw("COLZ");
    canv21->cd(n + 1 + glob->TELNumberOfPlanes);
    canv21->GetPad(n + 1 + glob->TELNumberOfPlanes)->SetGridx();
    canv21->GetPad(n + 1 + glob->TELNumberOfPlanes)->SetGridy();
    h21[1][n]->Draw("COLZ");

  }//n
  */
  //app.Run(true);
  //exit();
}
